<?php
class Commodity_m extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
    
    function get_commodity_info_c($commodity_code){
    	$header  = $this->db->get_where('commodity_header', array('COMMODITY_CODE'=>$commodity_code))->row();
    	if(count($header) < 1){
    		return null;
    	}
    	$detail  = $this->db->get_where('commodity_detail', array('COMMODITY_CODE'=>$commodity_code))->row();
    	if(count($detail) < 1){
    		return null;
    	}
    	 
    	return  array('detail' => $detail
    			, 'header' => $header
    	) ;
    }

    function searchList(){
    	$query = "SELECT * FROM COMMODITY_HEADER ";
    	return $this->db->query($query)->result();
    }

    function searchLists($arr,$start, $rp){
    	$query = "select ch.*,cd.* from commodity_header ch inner join commodity_detail cd
    			on ch.shop_code=cd.shop_code and ch.commodity_code = cd.commodity_code
    			where cd.shop_code = ?   " ;
    			
    	$param = array('0000');
    	if(!empty($arr["s_commoditycode_from"])) {
    		$query = $query." AND ch.commodity_code >=  ? ";
    		array_push($param,$arr["s_commoditycode_from"]);
    	}
    	if(!empty($arr["s_commoditycode_to"])) {
    		$query  = $query. " AND ch.commodity_code <=  ? ";
    		array_push($param,$arr["s_commoditycode_to"]);
    	}
    	if(!empty($arr["s_skucode"])) {
    		$query  = $query. " AND cd.sku_code = ? ";
    		array_push($param,$arr["s_skucode"]);
    	}else{
    		$query  = $query. " AND cd.sku_code = ch.represent_sku_code ";
    	}
    	
    	if(!empty($arr["s_commodityname"])) {
    		//$query  = $query. " AND CH.COMMODITY_NAME LIKE ? ";
    		//array_push($param,$arr["s_commodityname"]);
    	}

		echo $query;
    	var_dump($param);
    	
    	$query.= 'limit '.$start.', '.$rp;
    	
    	$rs = $this->db->query($query,$param);
    	var_dump($rs);
    	
    	//return $this->db->query($query,$param)->result();
    	return $rs->result();
    }

    function get_sql($arr,$is_count){

    	$query = "select count(ch.commodity_code) as cnt from commodity_header ch inner join commodity_detail cd
    			on ch.shop_code=cd.shop_code and ch.commodity_code = cd.commodity_code
    			where cd.shop_code = ?   " ;
    	
    	if (!$is_count){
    		$query = "select ch.*,cd.* from commodity_header ch inner join commodity_detail cd
    			on ch.shop_code=cd.shop_code and ch.commodity_code = cd.commodity_code
    			where cd.shop_code = ?   " ;
    	}
    	 
    	$param = array('0000');
    	if(!empty($arr["s_commoditycode_from"])) {
    		$query = $query." AND ch.commodity_code >=  ? ";
    		array_push($param,$arr["s_commoditycode_from"]);
    	}
    	if(!empty($arr["s_commoditycode_to"])) {
    		$query  = $query. " AND ch.commodity_code <=  ? ";
    		array_push($param,$arr["s_commoditycode_to"]);
    	}
    	if(!empty($arr["s_skucode"])) {
    		$query  = $query. " AND cd.sku_code = ? ";
    		array_push($param,$arr["s_skucode"]);
    	}else{
    		$query  = $query. " AND cd.sku_code = ch.represent_sku_code ";
    	}
    	 
    	if(!empty($arr["s_commodityname"])) {
    		//$query  = $query. " AND CH.COMMODITY_NAME LIKE ? ";
    		//array_push($param,$arr["s_commodityname"]);
    	}
    
    	$rtn = array($query,$param);
    	
    	return $rtn;
    	
//     	$query.= 'limit '.$start.', '.$rp;
//     	$rs = $this->db->query($query,$param);
//     	return $rs->result();
    }
    
    
	public function gets_total($arr) {
		$sql_set =  $this->get_sql($arr,true);
		$rs = $this->db->query($sql_set[0],$sql_set[1]);
		return $rs->row()->cnt;
		
	}
	
	public function get_commodity_lists($arr,$start, $rp){
		$sql_set =  $this->get_sql($arr,false);
		$query = $sql_set[0] . ' limit '.$start.', '.$rp;
		$rs = $this->db->query($query,$sql_set[1]);
		return $rs->result();
		
	}
    
    
    function search_detail($sku_code){
    	$query = "SELECT CH.*,CD.* FROM COMMODITY_HEADER CH INNER JOIN COMMODITY_DETAIL CD
    			ON CH.SHOP_CODE=CD.SHOP_CODE AND CH.COMMODITY_CODE = CD.COMMODITY_CODE
    			WHERE CD.SHOP_CODE = ? AND CD.COMMODITY_CODE = ? LIMIT 1 " ;
    	//return $this->db->query($query)->result();
    	//return $this->db->query($query, array($shop_code, '$sku_code))->result();
    	return $this->db->query($query, array('0000', $sku_code))->row();
    }
    
    function get_detail($sku_code){
    	
    	$this->db->where(array('sku_code' => $sku_code ));
    	$result = $this->db->get('commodity_detail', 1)->row();
    	return $result; 
    }
    function get_detail_commodity_code($commodity_code){
    	 
    	$this->db->where(array('commodity_code' => $commodity_code ));
    	$this->db->order_by('sku_code desc');
    	return  $this->db->get('commodity_detail')->row();
    }
    
    
    function get_header($commodity_code){
    	 
    	$this->db->where(array('commodity_code' => $commodity_code ));
    	return  $this->db->get('commodity_header', 1)->row();
    }
    
    
    function is_avaliavle($sku_code){
    	$detail = $this->get_commodity_detail($param['sku_code']);
    	
    	$header = $this->get_commodity_header($param['sku_code'],$detail['commodity_code']);
    	
    	//���J�L��
    	
    	//�݌Ƀ`�F�b�N
    	
    	//�w������`�F�b�N
    	
    	//�J�e�S���`�F�b�N
    }
    
}